package com.ts.proj.controller;

import com.ts.base.controller.BaseController;
import com.ts.common.constant.ConsoleConstant;
import com.ts.proj.entity.FinanceEntity;
import com.ts.proj.entity.ProjectEntity;
import com.ts.proj.entity.SystemUserEntity;
import com.ts.proj.json.FinanceJson;
import com.ts.proj.service.FinanceService;
import com.ts.proj.service.ProjectService;
import com.ts.util.JsonUtils;
import com.ts.util.JurisdictionUtil;
import com.ts.util.PageData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.web.ProjectedPayload;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

//说明 字符串：Test，作为替换字符串

@Controller
public class FinanceController extends BaseController {

    @Autowired
    private FinanceService financeService;
    @Autowired
    private FinanceJson financeJson;
    @Autowired
    private ProjectService projectService;
    @RequestMapping("/finance-add")
    public ModelAndView financeAddManage() {
        ModelAndView modelAndView = this.getModelAndView();
        String viewName = "login";
        String flag = "add";
        if(JurisdictionUtil.isValid()){
            HttpSession session = this.getRequest().getSession();
            SystemUserEntity user = JsonUtils.jsonToObject(session.getAttribute(ConsoleConstant.SESSIONUSER).toString(),SystemUserEntity.class);
            PageData pd = this.getPageData();
            FinanceEntity finance = new FinanceEntity();
            if(null!=pd.get("id")&&!"".equals(pd.getString("id"))){
                finance = financeService.findById(Integer.valueOf(pd.getString("id")));
                flag = "edit";
            }
            if("copy".equals(pd.getString("sql"))){
                finance.setId(-1);
            }
            List<PageData> proIdName = (List<PageData>) financeJson.idConversionName();

            modelAndView.addObject("idConversionName",proIdName);
            modelAndView.addObject("flag",flag);
            modelAndView.addObject("finance",finance);
            viewName = "business/finance/finance-add";
        }
        modelAndView.setViewName(viewName);
        return modelAndView;
    }

    @RequestMapping("/finance-update/{id}")
    public ModelAndView financeUpdateManage(@PathVariable String id) {
        ModelAndView modelAndView = this.getModelAndView();
        String viewName = "login";
        String flag = "update";
        if(JurisdictionUtil.isValid()){
            HttpSession session = this.getRequest().getSession();
            SystemUserEntity user = JsonUtils.jsonToObject(session.getAttribute(ConsoleConstant.SESSIONUSER).toString(),SystemUserEntity.class);
            PageData pd = this.getPageData();
            FinanceEntity finance = new FinanceEntity();
            ProjectEntity projectEntity = new ProjectEntity();
            if(null != id && !"".equals(id)){
                finance = financeService.findById(Integer.valueOf(id));
                flag = "edit";
            }
            projectEntity = projectService.findById(finance.getProId());
            if("copy".equals(pd.getString("sql"))){
                finance.setId(-1);
            }
            modelAndView.addObject("flag",flag);
            modelAndView.addObject("finance",finance);
            modelAndView.addObject("project",projectEntity);
            viewName = "business/finance/finance-update";
        }
        modelAndView.setViewName(viewName);
        return modelAndView;
    }
}